const service = require("./commentService.js");
const util = require("../../util.js");

var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    goodsId: '',
    dataSource: [],
    moreCount: 1,
    connect:'',
    totalCount: 0,
    startPage:0,
    pageCount: 0,
    prePage: 0,
    nextPage: 0,
    hasPrePage: false,
    hasNextPage: false,
    publishUserId:"",
    myHeader:"",
  },

  /**
   * 组件的方法列表
   */
  methods: {
    requestComment: function(id,addMore){wx.createSelectorQuery()
      var newSource = [];
      if (addMore == true){
          newSource = this.data.dataSource;
      }else(
        this.setData({
          startPage: 0
        })
      )
      let self = this;
      let data = {
        goodsId: id,
        startPage: self.data.startPage
      };
      self.setData({
        goodsId: id,
        myHeader: getApp().globalData.userInfo.avatarUrl
      });
      service.requestCommment(data,function(e){
        if (e != null){
          for (var i = 0; i < e.items.length; i++) {
            var item = e.items[i];
            let timeStap = util.timeToStamp(item.time);   
            console.log("================", item.time, timeStap)
            let time = util.getDateDiff(timeStap);
            item.time = time;
            newSource.push(item);
          }  
          self.setData({
            totalCount: e.totalCount,
            startPage: e.startPage,
            pageCount: e.pageCount,
            prePage: e.prePage,
            nextPage: e.nextPage,
            hasPrePage: e.hasPrePage,
            hasNextPage: e.hasNextPage,
            dataSource: newSource
          })
        }
      })
    },

    userInput: function (e) {
      console.log(e.detail.value);
      this.setData({
        connect: e.detail.value
      })
    },
    userComment: function(e){

      if (this.data.connect == "" || this.data.connect == null || this.data.connect == " "){
        wx.showToast({
          title: '评论内容不能为空',
          icon: "none",
        })
        return
      }
      this.setData({
        moreCount: 1,
      })
      let that = this;
      let data = { 
        'publishUserId': app.globalData.userId,
        "content": that.data.connect,
        "goodsId": that.data.goodsId
      };
      console.log(e,"1111111",data)
      service.addComment(data,function(e) {
        that.setData({
          connect: "",
        })
        that.requestComment(that.data.goodsId)
      });
    },
    //清空输入内容
    cleanInput: function (e) {
      var setMessage = { sendInfo: this.data.connect }
      this.setData(setMessage)
    },
    /**
     * 点击item头像跳转个人页面
     */
    clickHeadItem: function (data) {
      // wx.navigateTo({
      //   url: '/mineSubs/pages/homepage/personalhomepage?userId=' + data.currentTarget.dataset.itemdata.userId
      // })
    },

    biddresSeeMoreClick : function () {
      let count = this.data.moreCount + 1;
      this.setData({
        moreCount: count,
      })
      let that = this;
      if (this.data.hasNextPage){
        this.setData({
          startPage: this.data.nextPage
        })
      }
      console.log(this.data.moreCount)
      this.requestComment(this.data.goodsId,true)
    },
    likeRequest: function(e) {
      console.log(e)
      let data = e.currentTarget.dataset.itemdata;
      let index = e.currentTarget.dataset.index;

      if (data.likeCountFlag == true){
        return;
      }
      let self = this;
      app.func.Post(false, app.api.user_like,
        { "userId": app.globalData.userId, "relateId": data.id, flag: data.likeCountFlag ? '1' : '0' },
        function (data) {
          let item = self.data.dataSource[index]
          item.likeCount = item.likeCount + 1;
          item.likeCountFlag = true;
          self.data.dataSource[index] = item;
          self.setData({
            dataSource: self.data.dataSource
          })
        },
        function (error) {
            wx.showToast({
              title: '点赞失败',
              icon: "none",
            })
        })
    }
  }
})
